package dm.ui;

import java.awt.*;
import java.awt.event.*;


import javax.swing.JFrame;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.JToolBar;
import javax.swing.table.TableCellRenderer;
import java.util.ArrayList;
import dm.service.URLListener;
import dm.service.URLObject;
import dm.service.DownloadService;

class Main extends JFrame implements ActionListener, URLListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 51609808919L;	

	JMenuBar menubar;
	JToolBar toolbar;
	JMenu mnFile;
	JMenu mnAbout;
	JMenuItem mniExit;
	JMenuItem mniNewDowload;
	JMenuItem mniAbout;	
	JPanel pnCenter = null;
	JPanel pnSouth = null;
	
	private JTable tableProgress;
	private JScrollPane m_scrollPane;
	private JTableProgressModel tbModel;
	
	GridBagLayout m_gridBagLayout = new GridBagLayout();
 	GridBagConstraints m_gridBagConstraint = new GridBagConstraints();
	
	ArrayList<URLObject> arrURL = new ArrayList<URLObject>();
	String m_RootUrl = "";
	
	/*	Day la lop utility phuc vu cho viec hien thi frame
	 *  Purpose: Tranh viec goi setVisible cho jframe trong main thread	 
	 */
	private static class ShowMe implements Runnable
	{
		final JFrame frame;
		
		public ShowMe(JFrame frm)
		{
			this.frame = frm;
		}
		
		public void run()
		{
			frame.setVisible(true);
		}
	}//end class ShowMe
	
	
	public Main()
	{
		super();
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		menubar = new JMenuBar();
		toolbar = new JToolBar("Standard");		
		toolbar.setLayout(new FlowLayout());
		toolbar.setFloatable(false);		
		
		mnFile = new JMenu("File");
		mnAbout = new JMenu("Help");
		mniAbout = new JMenuItem("About Program");
		mniExit = new JMenuItem(" Exit program ");
		mniNewDowload = new JMenuItem(" New ");
		mniExit.addActionListener(this);
		mniNewDowload.addActionListener(this);
				
		mnFile.add(mniExit);
		mnFile.add(mniNewDowload);
		mnAbout.add(mniAbout);		
		
		
		menubar.add(mnFile);
		menubar.add(mnAbout);
		
		setJMenuBar(menubar);
				
		
		
		c.add(toolbar, BorderLayout.NORTH);				
		
		initTable(c);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
	}//end constructor	
	
	private void initTable(Container c)
	{
		try{			
			tbModel = new JTableProgressModel();
			
			TableCellRenderer defaultRenderer;
			tableProgress = new JTable(tbModel);
		    defaultRenderer = tableProgress.getDefaultRenderer(JProgressBar.class);
		    tableProgress.setDefaultRenderer(JProgressBar.class, new JTableProgressRenderer(defaultRenderer));
		    tableProgress.setPreferredScrollableViewportSize(new Dimension(400, 200));
		    m_scrollPane = new JScrollPane(tableProgress);		    
		    c.add(m_scrollPane);
		}catch(Exception ex)
		{
			System.out.println(ex.toString());
		}
	}
	
	public void addCom(Container ct, Component co, int c, int r, int nc, int nr, int ipadx, int ipady, int anchor, int fill)
 	{
 		m_gridBagConstraint.gridx = c;
 		m_gridBagConstraint.gridy = r;
 		m_gridBagConstraint.gridwidth = nc;
 		m_gridBagConstraint.gridheight = nr;
 		m_gridBagConstraint.fill = fill;
 		m_gridBagConstraint.ipadx = ipadx;
 		m_gridBagConstraint.ipady = ipady;
 		m_gridBagConstraint.anchor = anchor;
 		
 		ct.add(co, m_gridBagConstraint);
 	}
 	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==mniNewDowload)
		{
			NewDowload nd = new NewDowload();
			nd.setSize(500, 300);
			nd.setVisible(true);
			nd.addURLListener(this);
		}
		
		if(ae.getSource()==mniExit)
		{
			dispose();
			System.exit(0);
		}
	}//end actionPerformed	
	
	public void Finished(String msg)
	{
		// TODO Auto-generated method stub
		tbModel.fireTableDataChanged();
	}

	public void getNewURL(URLObject o, String si)
	{
		// TODO Auto-generated method stub
		JProgressBar pbar = null;
		
		synchronized(arrURL)
		{
	        if(o != null)
	        {	  
	        	MyTableRow pr = new MyTableRow();
	        	pbar = new JProgressBar(0, 100);
	    		pbar.setValue(0);
	    		pbar.setStringPainted(true);
	    		
	    		pr.setPbar(pbar);
	    		pr.setNumOfFile(0);
	    		pr.setTotalSize(0);
	    		pr.setUrlAddress(o.getUrl());
	    		
	    		this.tbModel.addRow(pr);
	    		
	    		System.out.println("[Main] " + o.getUrl() + " -- " + o.getFileName());
	    		
	        	DownloadService ds = new DownloadService(o, si, pr);
				ds.addURLListener(this);
				ds.start();
	        }
		}
	}
	
	// ======== main function ===============
	public static void main(String args[])throws Exception
	{		
		Main mainFrame = new Main();
		mainFrame.setSize(750, 400);
		mainFrame.setTitle("Multithreaded demo program");
		
		Runnable run = new ShowMe(mainFrame);
		EventQueue.invokeLater(run);
	}//end main function
}//end class MultiThreadGUI

////=================================================

